﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mimoza.Common;
using Mimoza.DBLayer.Transport;
using Mimoza.Transport;

namespace Mimoza.DBLayer.Transport.Client
{
    public class HostsRepository :
        ClientRepository<HostRecord>,
        IDBHostRepository
    {
        public HostsRepository()
        {
            HostsRepositoryTranferRequest.RegisterInTransport();
            RepositoryReply<HostRecord>.RegisterInTransport();
        }

        public HostRecord GetHost(Guid ID)
        {
            return MakeObjectRequest(
                    HostsRepositoryCustomRequest.CustomOperationType.GetHost, ID);
        }

        public HostRecord GetHost(string MACAddress)
        {
            return MakeObjectRequest(
                    HostsRepositoryCustomRequest.CustomOperationType.GetHostByMac,
                    MACAddress);
        }

        public HostRecord GetHostByName(string hostName)
        {
            return null;// MakeObjectRequest(
                     //HostsRepositoryCustomRequest.CustomOperationType.GetHostByMac,
                     //hostName);
        }

        public IEnumerable<PolicyRecord> GetPolicies(Guid hostID)
        {
            return null;
        }

        override public void Insert(HostRecord host)
        {
            host = MakeObjectRequest(new HostsRepositoryTranferRequest(Operation.Insert, host));
            base.Insert(host);
        }

        override public void Update(HostRecord host)
        {
            host = MakeObjectRequest(new HostsRepositoryTranferRequest(Operation.Update, host));
            base.Update(host);
        }

        override public void Delete(HostRecord host)
        {
            host = MakeObjectRequest(new HostsRepositoryTranferRequest(Operation.Delete, host));
            base.Delete(host);
        }

        HostRecord MakeObjectRequest(HostsRepositoryCustomRequest.CustomOperationType oper,
            Guid argument)
        {
            HostsRepositoryCustomRequest customReq = new HostsRepositoryCustomRequest(oper, argument.ToString());
            return MakeObjectRequest(new HostsRepositoryTranferRequest(customReq));
        }

        HostRecord MakeObjectRequest(HostsRepositoryCustomRequest.CustomOperationType oper,
            String argument)
        {
            HostsRepositoryCustomRequest customReq = new HostsRepositoryCustomRequest(oper, argument);
            return MakeObjectRequest(new HostsRepositoryTranferRequest(customReq));
        }


        ClientCollectionEnumerable<TCollectionRecord> MakeCollectionRequest<TCollectionRecord>(HostsRepositoryCustomRequest.CustomOperationType oper,
            Guid argument)
            where TCollectionRecord : class, new()
        {
            HostsRepositoryCustomRequest customReq = new HostsRepositoryCustomRequest(oper, argument.ToString());
            return MakeCollectionRequest<TCollectionRecord>(new HostsRepositoryTranferRequest(customReq));
        }
    }
}
